package com.example.HQCource;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zww
 * @since 2023-06-17
 * 1318497587
 * SecretId: AKID4m6csSrXsT4TkKsLmFuDV7VuDOPc91L4
 *
 * SecretKey: wndqzW0Ldmrqhoscb4ShkcER1ObvLG6W
 */
public class tx_cos {
    public static void main(String[] args) {
        // 初始化用户身份信息(secretId, secretKey)
        String secretId = "AKID4m6csSrXsT4TkKsLmFuDV7VuDOPc91L4";
        String secretKey = "wndqzW0Ldmrqhoscb4ShkcER1ObvLG6W";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-nanjing"));
// 配置使用 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
// 生成 cos 客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传的文件
        String localFilePath="C:\\Users\\小酷\\Desktop\\1.jpg";
        File localFile = new File(localFilePath);
// 指定文件将要存放的存储桶
        String bucketName = "zww-1318497587";
// 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = "2023/623/1.jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    }
}
